package com.rxnetwork.sample.module.index.activity;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.view.View;

import com.rxnetwork.sample.R;
import com.rxnetwork.sample.common.CommonFragmentAdapter;
import com.rxnetwork.sample.module.home.fragment.TestMVPFragment;
import com.rxnetwork.sample.module.index.contract.IndexContract;
import com.rxnetwork.sample.module.index.fragment.TestFragment;
import com.rxnetwork.sample.module.index.presenter.IndexPresenter;
import com.rxnetwork.sample.mvp.MVPBaseActivity;
import com.rxnetwork.sample.views.CommonImgTabLayout;
import com.rxnetwork.sample.views.CustomImgTabEntity;
import com.rxnetwork.sample.views.NoScrollViewPager;
import com.rxnetwork.sample.views.TabEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class IndexActivity extends MVPBaseActivity<IndexContract.View, IndexPresenter> implements IndexContract.View {

    @BindView(R.id.view_pager)
    NoScrollViewPager mViewPager;
    @BindView(R.id.tab_layout)
    CommonImgTabLayout mTabLayout;

    private CommonFragmentAdapter mAdapter;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_index;
    }


    @Override
    protected void initViewsAndEvents() {
        setTopBarLayoutColor(Color.parseColor("#e71f19"));
        readLocalTabBarCache();
        getTopBarLayout().getLeftImg().setVisibility(View.GONE);
    }

    private void initViews(List<CustomImgTabEntity> list, List<Fragment> fragments) {
        mTabLayout.setTabData((ArrayList<CustomImgTabEntity>) list);
        mTabLayout.setOnTabSelectListener(new com.flyco.tablayout.listener.OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position, false);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        //70x56
        mTabLayout.setIconWidth(20);
        mTabLayout.setIconHeight(20);
        mAdapter = new CommonFragmentAdapter(getSupportFragmentManager(),
                fragments);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(list.size() - 1);
    }

    private void readLocalTabBarCache() {
        List<CustomImgTabEntity> list = new ArrayList<>();
        String[] array = getResources().getStringArray(R.array.home_tab_title);
        list.add(new TabEntity(array[0], R.mipmap.tab_index_click, R.mipmap.tab_index));
        list.add(new TabEntity(array[1], R.mipmap.tab_type_click, R.mipmap.tab_type));
        list.add(new TabEntity(array[2], R.mipmap.tab_shop_cart_click, R.mipmap.tab_shop_cart));
        list.add(new TabEntity(array[3], R.mipmap.tab_myself_click, R.mipmap.tab_myself));
        initViews(list, getFragments());
    }

    @Override
    protected CharSequence getTopTitle() {
        return null;
    }

    private List<Fragment> getFragments() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new Fragment());
        fragments.add(new TestFragment());
        fragments.add(new Fragment());
        fragments.add(new Fragment());
        return fragments;
    }

}
